const Proxy = require('../proxy')

const ErrMsgProxy = Proxy.ErrMsg
function errorToString (error) {
  return `${error.name}: ${error.message}${error.stack}`
}
exports.addErrMsg = async function (err) {
  let text = ''
  if (err && err.name) {
    text = errorToString(err)
  } else {
    text = err.toString()
  }
  return ErrMsgProxy.newAndSave({
    text: text
  })
}

exports.deleteErrMsg = async function () {
}

exports.updateErrMsg = async function () {
}

exports.getErrMsg = async function () {
}

exports.getErrMsgs = async function () {
}

exports.getErrMsgsByPaging = async function (query, paging) {
  const opt = {
    skip: paging.start,
    limit: paging.offset,
    sort: '-create_at'
  }
  const queryOption = {}
  const data = await Promise.all([
    ErrMsgProxy.find(queryOption, opt),
    ErrMsgProxy.count(queryOption)
  ])
  return { list: data[0], count: data[1] }
}
